package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //提供一个方法处理查询所有部门的请求,响应HTML
    @RequestMapping("/list")
   // @RequiredPermission(name = "部门列表")
    @RequiresPermissions(value = {"systemDictionary:list","部门列表"}, logical=Logical.OR)//必须要有指定的权限才能访问该方法,所以之前的注解可以删了
    //要贴上注解,因为页面要取值的话不贴@ModelAttribute去到的值是${queryObject.xxx},首字符会小写
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {//创建qo对象
        //这里不再是调用listAll的方法,而是我嗯创建的query方法
        PageInfo pageInfo = systemDictionaryService.query(qo);//这里没有qo对象需要我们在上面创建,返回的方法是pageResult
        model.addAttribute("result", pageInfo);
        return "systemDictionary/list";// /WEB-INF/views/systemDictionary/potentialList.ftl
    }



    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"saveOrUpdate:list","部门保存/更新"}, logical=Logical.OR)//or只要有一个就可以运行
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {

        if (systemDictionary.getId() == null) {
            systemDictionaryService.save(systemDictionary);
        } else {
            systemDictionaryService.update(systemDictionary);
        }
        return new JsonResult();

    }

}
